package com.opgea.crm.web.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.vo.CommonAppvo;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.services.SetupService;

@Controller
@RequestMapping("setup")
public class SetupsController {
	
	Logger logger=Logger.getLogger(SetupsController.class);
	@Autowired
		private SetupService setupService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String show(HttpServletRequest request){
		return "setups";
	}
	
	@RequestMapping(value="listsetups",method=RequestMethod.GET)
	public @ResponseBody 
	Map<String, List<CommonAppvo>> listModules(){
		List<CommonAppvo> list=setupService.listSetupModules();
		Map<String, List<CommonAppvo>> setupMap = new HashMap<String, List<CommonAppvo>>();
		setupMap.put("data", list);
		return setupMap;
		
	}
	
	@RequestMapping(value="listAttributes",method=RequestMethod.GET)
	public @ResponseBody 
	Map<String,List<CommonAppvo>>listModuleAttributes(@RequestParam(required=false,defaultValue="0") Integer moduleid)
	{
		List<CommonAppvo> list=null;
		
			list=setupService.listSetupAttributesByModuleid(moduleid);
	
		Map<String, List<CommonAppvo>> setupMap = new HashMap<String, List<CommonAppvo>>();
		setupMap.put("data", list);
		return setupMap;
		
	}
	
	
	@RequestMapping(value="listAttributeValues",method=RequestMethod.GET)
	public @ResponseBody 
	Map<String,List<SetupsValue>> listAttributeValues(
													@RequestParam(required=false) Integer setupModuleid,
													@RequestParam(required=false) Integer setupAttributeid)
	{
		List<SetupsValue> list=null;
		
			if(setupModuleid!=null && setupAttributeid!=null)
			list=setupService.listSetupValue(setupModuleid,setupAttributeid);
			else{
				list=setupService.listSetupValue(0,0);
			}
		
		Map<String, List<SetupsValue>> setupMap = new HashMap<String, List<SetupsValue>>();
		setupMap.put("data", list);
		return setupMap;
		
	}
	
	@RequestMapping(value="saveAttrvalue",method=RequestMethod.POST)
	public @ResponseBody 
	void saveAttributeValue(@RequestParam(required=false) String setupAttrValue,
							@RequestParam(required=false) String operation,
							@RequestParam(required=false) Long setupValueid,
							@RequestParam(required=false) Integer setupModuleid,
							@RequestParam(required=false) Integer setupAttributeid)
	{
		logger.debug("Init saveAttributeValue for "+operation);
		if(StringUtils.equalsIgnoreCase(operation,"save"))
				setupService.saveAttrValue(setupModuleid,setupAttributeid,setupAttrValue);
		if(StringUtils.equalsIgnoreCase(operation,"remove"))
			setupService.removeAttrValue(setupValueid);
		
	}
	

}
